package regex;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class RegexExample {
   public static void main(String[] args){
//      String content = "I am noob from runoob.com.";
//
//      String pattern = ".*runoob.*"; //是否包含runoob
//      //分组提取
//      String pattern2 = "(.*)runoob(.*)"; //是否包含runoob
//      Pattern r = Pattern.compile(pattern2);
//
//      Matcher matcher = r.matcher(content);
//
//      if(matcher.find()){
//         String group = matcher.group();
//         String group1 = matcher.group(1);
//         String group2 = matcher.group(2);
//         System.out.println(group);
//         System.out.println(group1);
//         System.out.println(group2);
//      }

      testReg();
      //boolean isMatch = Pattern.matches(pattern, content);
     // System.out.println("字符串中是否包含了 'runoob' 子字符串? " + isMatch);
   }

   private static void testReg() {
      Map<String,Object> map =new HashMap<>();
      map.put("loginname","abc");
      map.put("loginpass","abc");
      //String str="http://82.156.80.137:8080/goods/UserServlet?method=loginMobile&loginname=${loginname}";

      String str="http://82.156.80.137:8080/goods/UserServlet?method=loginMobile&loginname=${loginname}&loginpass=${loginpass}";
      String pattern2 = "\\$\\{(.*?)\\}"; //是否包含runoob 特殊要额外处理\\ 贪婪匹配=最大匹配 非贪婪 ${.*?}
      Pattern r = Pattern.compile(pattern2);
      Matcher matcher = r.matcher(str);
      while(matcher.find()){ //匹配多个
         String group = matcher.group(0);
         String group1 = matcher.group(1);
         System.out.println(group+" group1 "+group1);
         str=str.replace(group,String.valueOf(map.get(group1)));
      }
      System.out.println(str);


   }


}
